package com.talent.controller;

import com.talent.entities.dto.Vacc;
import com.talent.entities.dto.VaccOrder;
import com.talent.service.PaymentSericve;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.ui.Model;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.LinkedList;

/**
 * @author 周意朗
 * Date: 2022/2/22
 * @since JDK 11
 */

@RestController
@Slf4j
@EnableAutoConfiguration
@CrossOrigin
public class PaymentController {
    @Value("${server.port}")
    private String serverPort;

    @Resource
    PaymentSericve sericve;

    @GetMapping(value = "/payment/{id}")
    public String getReady(@PathVariable("id") Long id,
                           HttpServletRequest request,
                           Model model, HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Cache-Control", "no-cache");
        return "我是 nacos 注册,port:" + serverPort + "\t id:" + id;
    }

    @PostMapping("order/selectfavorite")
    public LinkedList<Vacc> selectFavorite(@RequestBody String uid) {
        return sericve.selectFavorite(uid);
    }

    @PostMapping("order/addfavorite")
    public String addFavorite(@RequestBody MultiValueMap<String, String> map) {

        String userid = map.getFirst("userid");
        String vaccid = map.getFirst("vaccid");

        return sericve.addFavorite(userid, vaccid);
    }

    @PostMapping("order/deletefavorite")
    public String deleteFavorite(@RequestBody MultiValueMap<String, String> map) {
        String userid = map.getFirst("userid");
        String vaccid = map.getFirst("vaccid");
        sericve.deleteFavorite(userid, vaccid);
        return sericve.deleteFavorite(userid, vaccid);
    }

    @PostMapping("/order/createorder/")
    public Boolean createOrder(@RequestBody VaccOrder vaccOrder,
                               HttpServletRequest request,
                               HttpServletResponse response) {

        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        return sericve.createOrder(vaccOrder);
    }

    @PostMapping("/order/finishorder/")
    public Boolean finishOrder(@RequestBody String id,
                               HttpServletRequest request,
                               HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        response.setHeader("Access-Control-Allow-Methods", "*");
        response.setHeader("Cache-Control", "no-cache");
        response.setHeader("Access-Control-Allow-Credentials", "true");
        response.setHeader("Access-Control-Max-Age", "3600");
        return sericve.finishOrder(id);
    }

    @PostMapping("order/selectorder")
    public LinkedList<VaccOrder> selectOrder(@RequestBody String id) {
        return sericve.selectOrder(id);
    }


}
